using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using TomShane.Neoforce.Controls;
using Warlock;
using Warlock.Input;
using Warlock.Menu;

namespace Warlock
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        IGameScreen gameScreen;

        /// <summary>
        /// Singleton
        /// Der Manager, der alle NeoForce-Windows kontrolliert. In UnloadContent immer den Manager leeren (manager.Remove(window))!
        /// </summary>
        public static Manager Manager;

        Warlock.Menu.MainMenu mainMenu;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = 768;
            graphics.PreferredBackBufferWidth = 1024;
            graphics.ApplyChanges();
            graphics.PreparingDeviceSettings += new
                EventHandler<PreparingDeviceSettingsEventArgs>(OnPreparingDeviceSettings);


            Content.RootDirectory = "Content";

            Manager = new Manager(this, graphics);
            Manager.InputEnabled = true;
            Manager.SkinDirectory = "Content\\Skins\\";

            mainMenu = new Warlock.Menu.MainMenu(this);

        }

        void OnPreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
        {
            foreach (GraphicsAdapter adapter in GraphicsAdapter.Adapters)
            {
                if (adapter.Description.Contains("NVIDIA PerfHUD"))
                {
                    e.GraphicsDeviceInformation.Adapter = adapter;
                    e.GraphicsDeviceInformation.DeviceType = DeviceType.Reference;
                    break;
                }
            }
        }



        protected override void Initialize()
        {
            base.Initialize();

            SwitchGamescreen(mainMenu);
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        protected override void UnloadContent()
        {
            gameScreen.UnloadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            //InputManager.Instance.Update();
            gameScreen.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            base.Draw(gameTime);

            gameScreen.Draw(spriteBatch, gameTime);
        }
        public void SwitchGamescreen(IGameScreen newScreen)
        {
            if (gameScreen != null)
                gameScreen.UnloadContent();
            gameScreen = newScreen;
            gameScreen.Initialize(graphics, Content);
        }
    }
}